var app = getApp()

Page({
  data: {
    stepIndex: 0,
    password: '',
    sumsets: ['50元', '100元', '150元', '200元'],
    freeAmount: 0,
    sums: [50, 100, 150, 200],
    sumIndex: 0,
    freeOpen: true
  },
  forgetPwd: function () {
    var phone = app.globalData.personInfo.MOBILE
    var msg = '发送（临时密码）到您的激活手机号码：' + phone
    var that = this
    wx.showModal({
      title: '',
      content: msg,
      confirmColor: '#a08257',
      confirmText: '获取密码',
      cancelText: '关闭',
      success: function (res) {
        if (res.confirm) {
          that.sendMsgAction()
        }
      }
    })
  },
  sendMsgAction: function () {
    var phone = app.globalData.personInfo.MOBILE
    var userid = app.globalData.userid
    var url = '/yuanmai_we/user/password/forgot'
    var property = {
      "USERID": userid,
      "MOBILE": phone
    }
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    }
    app.request(url, params).then(res => {
      if (res.success) {
        app.toast.success('发送成功')
      }
    })
  },
  goBack: function () {
    wx.navigateBack()
  },
  // 确定免密支付
  confirmFreePay: function () {
    if (this.data.password === '') {
      return app.toast.error('请输入密码')
    }
    var url = '/yuanmai_we/user/password/verify'
    var userid = app.globalData.userid
    var property = {
      "USERID": userid,
      "PASSWORD": this.data.password
    }
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    }
    app.request(url, params).then(res => {
      if (res.success) {
        this.getBalanceSum()
      } else {
        app.toast.error(res.errmsg)
      }
    })
  },
  // 获取免密金额
  getBalanceSum: function () {
    app.showLoading()
    var url = '/yuanmai_we/user/balance/setting/get'
    app.get(url).then(res => {
      app.hideLoading()
      var freeOpen = res.freeSecret == 1 ? true : false
      this.setData({
        stepIndex: 1,
        freeAmount: res.freeAmount,
        freeOpen: freeOpen
      })
    })
  },
  // 提交设置免密支付
  submitPayFree: function () {
    var freeSecret = this.data.freeOpen == true ? 1 : 0
    var property = {
      "freeSecret": freeSecret,
      "freeAmount": this.data.freeAmount
    }
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    }
    var url = '/yuanmai_we/user/balance/setting'
    app.request(url, params).then(res => {
      if (res.status) {
        app.toast.success('设置成功')
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      }
    })
  },
  bindSumChange: function (e) {
    this.setData({
      sumIndex: e.detail.value
    })
  },
  switchChange: function (e) {
    this.setData({
      freeOpen: e.detail.value
    })
  },
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  bindAmountChange: function (e) {
    this.setData({
      freeAmount: this.data.sums[e.detail.value]
    })
  }
})
